package com.example.lib_repair

import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.widget.CheckBox
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.PopupWindow
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.SimpleItemAnimator
import androidx.viewbinding.ViewBinding
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ToastUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemChildClickListener
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.example.lib_common.mvi.BaseActivity
import com.example.lib_repair.databinding.ActivityRepairBinding
import com.example.lib_repair.detail.UserAdapter
import com.example.lib_repair.detail.UserEntity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@Route(path = "/repair/RepairActivity")
class RepairActivity : BaseActivity<ActivityRepairBinding,RepairViewModel>() {
    var repairList : ArrayList<RepairEntity> = ArrayList()
    val repairAdapter =RepairAdapter()
    val userAdapter = UserAdapter()
    lateinit var inflate : View
    override fun createVM(): RepairViewModel {
        return RepairViewModel()
    }

    override fun createViewBinding(): ViewBinding {
        return ActivityRepairBinding.inflate(inflater)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        bindState()
        vm.requestAll()
        bindAdapter()
        v.backBtn.setOnClickListener { finish() }
        v.repairManage.setOnClickListener {
            vm.requestUser()
             inflate = LayoutInflater.from(this).inflate(R.layout.item_manage, null, false)
            for (repairEntity in repairList) {
                repairEntity.isSee=true
            }
            repairAdapter.notifyDataSetChanged()
            val popupWindow = PopupWindow()
            popupWindow.isFocusable=false
            popupWindow.width= LinearLayout.LayoutParams.MATCH_PARENT
            popupWindow.height=400
            popupWindow.contentView=inflate
            popupWindow.showAtLocation(v.repairManage, Gravity.BOTTOM,0,0)
            inflate.findViewById<CheckBox>(R.id.item_manage).setOnCheckedChangeListener { compoundButton, b ->
                if (b){
                    for (repairEntity in repairList) {
                        repairEntity.isFlag=true
                    }
                    repairAdapter.notifyDataSetChanged()
                }else{
                    for (repairEntity in repairList) {
                        repairEntity.isFlag=false

                    }
                    repairAdapter.notifyDataSetChanged()
                }
            }

            inflate.findViewById<ImageView>(R.id.item_manage_close).setOnClickListener {
                popupWindow.dismiss()
                for (repairEntity in repairList) {
                    repairEntity.isSee=false
                }
                repairAdapter.notifyDataSetChanged()
            }
            val repair_recycler = inflate.findViewById<RecyclerView>(R.id.item_repairuser_recycler)
            val layoutManager = LinearLayoutManager(this)
            layoutManager.orientation= RecyclerView.HORIZONTAL
            repair_recycler.layoutManager=layoutManager
            repair_recycler.adapter=userAdapter
            var userList : ArrayList<UserEntity> =ArrayList()
            userAdapter.setOnItemClickListener(object : OnItemClickListener{
                override fun onItemClick(adapter: BaseQuickAdapter<*, *>,view: View,position: Int,
                ) {
                    userList= adapter.data as ArrayList<UserEntity>
                    userList[position].uName
                }
            })
        }
    }
    private fun bindState() {
        lifecycleScope.launch(Dispatchers.Main){
            repeatOnLifecycle(Lifecycle.State.STARTED){
                vm.mutableContainer.uiStateFlow.collect{
                    if (it.entity.statuesCode==200){
                        repairList= it.entity.values as ArrayList<RepairEntity>
                        repairAdapter.setNewInstance(repairList)
                    }
                    if (it.entity3.statuesCode==200){
                        userAdapter.setNewInstance(it.entity3.values as ArrayList<UserEntity>)
                    }
                }
            }
        }
    }
    var list :ArrayList<RepairEntity> = ArrayList()
    private fun bindAdapter() {
        var layoutManager = LinearLayoutManager(this)
        layoutManager.orientation=LinearLayoutManager.VERTICAL
        v.repairRecycler.layoutManager=layoutManager
        v.repairRecycler.adapter=repairAdapter
        repairAdapter.setOnItemClickListener { adapter, view, position ->
            list = adapter.data as ArrayList<RepairEntity>
            ARouter.getInstance().build("/repair/DetailRepair")
                .withString("page", "维修")
                .withSerializable("repair", list[position]).navigation()
        }
        repairAdapter.setOnItemChildClickListener { adapter, view, position ->
            ToastUtils.showShort("" + position)
            LogUtils.e("=====position${position}")
            var count = 0
            for (repairEntity in repairList) {
                if (repairEntity.isFlag == true) {
                    count++
                }
            }
            LogUtils.e("======count${count}")
            if (count == repairList.size) {
                inflate.findViewById<CheckBox>(R.id.item_manage).isChecked = true
            } else {
                inflate.findViewById<CheckBox>(R.id.item_manage).isChecked = false
            }
        }
    }
}